package com.xltt.watchlauncher3.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.xltt.watchlauncher3.R;
import com.xltt.watchlauncher3.entity.AppInfoEntity;
import com.xltt.watchlauncher3.view.menu.AppInfoEnumerator;
import com.xltt.watchlauncher3.view.menu.CenterSnapHelper;
import com.xltt.watchlauncher3.view.menu.LauncherAdapter;
import com.xltt.watchlauncher3.view.menu.LayoutManager;
import com.xltt.watchlauncher3.view.menu.ScrollHelper;
import com.xltt.watchlauncher3.view.menu.Util;
import com.xltt.watchlauncher3.view.menu.ViewPagerLayoutManager;

import java.util.List;



public class ListLauncherActivity extends BaseActivity implements LauncherAdapter.OnItemClickListener, ViewPagerLayoutManager.OnPageChangeListener {

    private RecyclerView mRecyclerView;
    private ProgressBar mProgressBar;

    private LauncherAdapter mLauncherAdapter;
    private AppInfoEnumerator mAppInfoEnumerator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher_layout);
        initViews();
        mProgressBar.setVisibility(View.GONE);
        loadApp();
    }

    private void initViews(){
        mRecyclerView = findViewById(R.id.recycler);
        mProgressBar = findViewById(R.id.progressView);
    }

    private void loadApp() {
        mAppInfoEnumerator = new AppInfoEnumerator(this, Intent.CATEGORY_LAUNCHER,
                new AppInfoEnumerator.CompleteCallback() {
                    @Override
                    public void onComplete(List<AppInfoEntity> results) {
                        initData(results);
                    }
                });
        mAppInfoEnumerator.execute(0);
    }

    private void initData(List<AppInfoEntity> datas) {
        mProgressBar.setVisibility(View.GONE);

        mLauncherAdapter = new LauncherAdapter(datas);
        mLauncherAdapter.setOnItemClickListener(this);
        LayoutManager mCarouselLayoutManager = new LayoutManager(this, Util.Dp2px(this, 50), ViewPagerLayoutManager.VERTICAL);
        mCarouselLayoutManager.setOnPageChangeListener(this);
        mRecyclerView.setAdapter(mLauncherAdapter);
        mRecyclerView.setLayoutManager(mCarouselLayoutManager);
        CenterSnapHelper centerSnapHelper = new CenterSnapHelper();
        centerSnapHelper.attachToRecyclerView(mRecyclerView);
//        centerSnapHelper.snapToCenterView(mCarouselLayoutManager,this);
    }

    @Override
    public void onItemClick(View v, int pos) {
        List<AppInfoEntity> appInfoArrays = mLauncherAdapter.getDatas();
        AppInfoEntity info = appInfoArrays.get(pos);
        startActivity(info.getIntent());
        ScrollHelper.smoothScrollToTargetView(mRecyclerView, v);
    }


    @Override
    protected void onDestroy() {
        if (mAppInfoEnumerator != null && mAppInfoEnumerator.getStatus() == AsyncTask.Status.RUNNING) {
            mAppInfoEnumerator.cancel(true);
        }
        super.onDestroy();
    }

    @Override
    public void onPageSelected(int position) {
        mLauncherAdapter.setPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
